package cn.edu.zust.se.service.user.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.edu.zust.se.api.clients.DiskClient;
import cn.edu.zust.se.api.clients.FileClient;
import cn.edu.zust.se.api.entity.User;
import cn.edu.zust.se.api.entity.bo.FileBo;
import cn.edu.zust.se.api.entity.bo.FolderBo;
import cn.edu.zust.se.api.entity.bo.UserBo;
import cn.edu.zust.se.api.entity.vo.FileVo;
import cn.edu.zust.se.api.entity.vo.UserVo;
import cn.edu.zust.se.api.enums.RedisKeyEnum;
import cn.edu.zust.se.api.enums.UserStatusEnum;
import cn.edu.zust.se.api.enums.UserTypeEnum;
import cn.edu.zust.se.service.user.mapper.UserMapper;
import cn.edu.zust.se.service.user.service.UserService;
import cn.edu.zust.se.service.user.utils.RedisUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private final UserMapper userMapper;
    private final DiskClient diskClient;
    private final FileClient fileClient;

    @Override
    public UserVo getUserById(Long id) {
        User user = userMapper.selectById(id);
        return BeanUtil.copyProperties(user, UserVo.class);
    }

    @Override
    public UserVo doLogin(UserBo bo) {
        User user = userMapper.selectOne(
            new LambdaQueryWrapper<User>().eq(User::getUsername, bo.getUsername()));
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (user != null) {
            UserStatusEnum statusEnum = UserStatusEnum.getEnumByCode(user.getStatus());
            if (statusEnum != null && (statusEnum.equals(UserStatusEnum.NORMAL))) {
                if (encoder.matches(bo.getPassword(), user.getPassword())) {
                    RedisUtil.set(RedisKeyEnum.AUTH_CODE.getPrefix() + user.getId(), BeanUtil.copyProperties(user, UserVo.class));
                    StpUtil.login(user.getId());
                    return BeanUtil.copyProperties(user, UserVo.class);
                } else {
                    return null;
                }
            }
            return null;
        } else {
            return null;
        }
    }
    //更新用户信息
    @Override
    public Boolean updateUser(UserBo bo) {
        bo.setId(StpUtil.getLoginIdAsLong());
        //更新后密码加密
        if (bo.getPassword() != null)
        {
            bo.setPassword(new BCryptPasswordEncoder().encode(bo.getPassword()));
        }
        return userMapper.updateById(BeanUtil.copyProperties(bo, User.class)) > 0;
    }

    @Override
    public Boolean doRegister(UserBo bo) {
        bo.setType(UserTypeEnum.USER.getCode());
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, bo.getUsername());
        User user = userMapper.selectOne(lqw);
        if (user != null) {
            return false;
        } else {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String encodePassword = encoder.encode(bo.getPassword());
            bo.setPassword(encodePassword);
            bo.setStatus(UserStatusEnum.NORMAL.getCode());
            User user1 = BeanUtil.copyProperties(bo, User.class);
            user1.setFileId(1741765159086280705L);
            userMapper.insert(user1);
            FolderBo folderBo = new FolderBo();
            folderBo.setUserId(user1.getId());
            folderBo.setName("主页");
            folderBo.setParentId(0L);
            fileClient.addFolder(folderBo);
            //注册成功后自动创建用户的网盘
           return diskClient.addDisk(user1.getId()).getData();
        }
    }
}
